#include <raylib.h>
#include <raymath.h>

#include "geography.h"

int main(void) {
	// Initialization
	//--------------------------------------------------------------------------------------

	SetConfigFlags(FLAG_MSAA_4X_HINT);  // Enable Multi Sampling Anti Aliasing 4x (if available)
	int screenWidth = 1920;
	int screenHeight = 1080;
	InitWindow(screenWidth, screenHeight, "raylib shader");

	// Define the camera to look into our 3d world

	// Load plane model from a generated mesh
//    Model model = LoadModelFromMesh(GenMeshCube(10.0f, 10.0f, 3.3));

//    Shader shader = LoadShader("vertices_shader.vs","fragment_shader.fs");
	// Assign out lighting shader to model
//    model.materials[0].shader = shader;

//    SetCameraMode(camera, CAMERA_ORBITAL);  // Set an orbital camera mode

	SetTargetFPS(60);                       // Set our game to run at 60 frames-per-second
	SetTraceLogLevel(LOG_WARNING);
	//--------------------------------------------------------------------------------------
	Geography geography;
	geography.init();
	auto start = std::chrono::steady_clock::now();
	std::chrono::nanoseconds delt = start - start;
	geography.update(delt);
	Vector3 earthPos = geography.getEarthPos();

	Camera camera1 = { 0 };    // Camera looking at point
	Camera camera2 = {0};
	Camera camera3 = { 0 };    // Camera looking at point
	Camera camera4 = {0};
	camera1.target = earthPos;
	Vector3 earthPosNormal = Vector3Normalize(earthPos);
	Vector3 deltPos = Vector3Add(Vector3Scale(earthPosNormal, 0.06), {-0.01, -0.01, 0.02});
	camera1.position = Vector3Subtract(earthPos, deltPos);
	camera1.up = (Vector3) {
		0.0f, 0.0f, 1.0f
	};          // Camera up vector (rotation towards target)
	camera1.fovy = 45.0f;                                // Camera field-of-view Y
	camera1.projection = CAMERA_PERSPECTIVE;             // Camera mode type
	RenderTexture screen1 = LoadRenderTexture(screenWidth / 2, screenHeight / 2);
	RenderTexture screen2 = LoadRenderTexture(screenWidth / 2, screenHeight / 2);
	camera2 = camera1;
	RenderTexture screen3 = LoadRenderTexture(screenWidth / 2, screenHeight / 2);
	camera3 = camera1;
	RenderTexture screen4 = LoadRenderTexture(screenWidth / 2, screenHeight / 2);
	camera4 = camera1;

	Rectangle splitScreenRect = { 0.0f, 0.0f, (float)screen1.texture.width, (float)screen1.texture.height };
	// Main game loop
	while (!WindowShouldClose()) {          // Detect window close button or ESC key
		//----------------------------------------------------------------------------------
		earthPos = geography.getEarthPos();
		camera1.target = earthPos;
		camera2.target = earthPos;
		camera3.target = earthPos;
		camera4.target = earthPos;
		Vector3 earthPosNormal = Vector3Normalize(earthPos);
		Vector3 litterEarthPosNormal = Vector3Scale(earthPosNormal, 0.006);
		Vector3 deltPos = Vector3Add(litterEarthPosNormal, {-0.04, 0.00, 0.06});
		camera1.position = Vector3Subtract(earthPos, deltPos);
		Vector3 deltPos2 = Vector3Add(litterEarthPosNormal, {0.04, 0.00, 0.00});
		camera2.position = Vector3Subtract(earthPos, deltPos2);
		Vector3 deltPos3 = Vector3Add(litterEarthPosNormal, {0.04, -0.04, 0.02});
		camera3.position = Vector3Subtract(earthPos, deltPos3);
		Vector3 deltPos4 = Vector3Add(litterEarthPosNormal, {0.04, 0.04, 0.02});
		camera4.position = Vector3Subtract(earthPos, deltPos4);
		// Draw
		//----------------------------------------------------------------------------------
		auto end = std::chrono::steady_clock::now();
		geography.update(end - start);
		start = end;

		BeginTextureMode(screen1);
		ClearBackground(BLACK);
		BeginMode3D(camera1);
		geography.draw();
		EndMode3D();
		EndTextureMode();

		BeginTextureMode(screen2);
		ClearBackground(BLACK);
		BeginMode3D(camera2);
		geography.draw();
		EndMode3D();
		EndTextureMode();

		BeginTextureMode(screen3);
		ClearBackground(BLACK);
		BeginMode3D(camera3);
		geography.draw();
		EndMode3D();
		EndTextureMode();

		BeginTextureMode(screen4);
		ClearBackground(BLACK);
		BeginMode3D(camera4);
		geography.draw();
		EndMode3D();
		EndTextureMode();

		BeginDrawing();

		ClearBackground(BLACK);

		DrawTextureRec(screen1.texture, splitScreenRect, (Vector2) {
			0.0f, 0.0f
		}, WHITE);
		DrawTextureRec(screen2.texture, splitScreenRect, (Vector2) {
			screenWidth / 2.0f, 0
		}, WHITE);
		DrawTextureRec(screen3.texture, splitScreenRect, (Vector2) {
			0, screenHeight/2.0f
		}, WHITE);
		DrawTextureRec(screen4.texture, splitScreenRect, (Vector2) {
			screenWidth / 2.0f, screenHeight / 2.0f
		}, WHITE);

		EndDrawing();
		//----------------------------------------------------------------------------------
	}

//    UnloadModel(model);     // Unload the model
//    UnloadShader(shader);   // Unload shader
	UnloadRenderTexture(screen1); // Unload render texture
	UnloadRenderTexture(screen2); // Unload render texture
	CloseWindow();          // Close window and OpenGL context
	//--------------------------------------------------------------------------------------

	return 0;
}
